package com.zhdl.modules.eastdosing;

import cn.hutool.core.util.HexUtil;
import com.project.common.util.DelayUtil;
import com.zhdl.hardware.io.smartLink.repository.impl.IOCmdHandleDaoImpl;
import com.zhdl.hardware.io.smartLink.service.IOCmdSendService;
import com.zhdl.hardware.meich_camera.MeichCameraCmdHandleDao;
import com.zhdl.hardware.motor.buke.repository.impl.BukePortCmdHandle;
import com.zhdl.hardware.ranging.myantenna.repository.impl.MTPortCmdHandle;
import com.zhdl.hardware.ranging.myantenna.service.MTRangingCmdSendService;
import com.zhdl.hardware.robot.borunte.repository.impl.BorunteCmdHandleDaoImpl;
import com.zhdl.hardware.robot.borunte.service.BorunteCmdSendService;
import com.zhdl.hardware_modules.entity.DeviceAction;
import com.zhdl.hardware_modules.service.DeviceService;
import com.zhdl.hardware_modules.web.service.HardwareDebugService;
import com.zhdl.modules.autopick.repository.CameraRepository;
import com.zhdl.modules.autopick.web.response.NewMemDevice;
import com.zhdl.modules.eastdosing.entity.EastDataResp;
import com.zhdl.modules.eastdosing.service.CameraEastService;
import com.zhdl.modules.tools.CommonCallback;
import com.zhdl.modules.tools.ProcessTask;
import com.zhdl.network.asclltcp.AsciiAfterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class EastDosingProcess {

    /**
     * 主进度 流程进度 - 机器人
     * 分支进度 流程进度 - 电机
     * 报警ID 料斗报警 南-北
     */
    public static int processRobot = -1, processMotor = -1, warnId = -1;
    /**
     * 当前流程是否在运行 - 默认未运行
     * 当前流程是否在测试 - 默认未测试
     * true 运行 false 未运行
     */
    public static boolean isRunning = false, isTest = false, isStart = false;
    /**
     * 是否初始化完成 --- 机器人
     * 是否初始化完成 --- 电机
     */
    public boolean robotInit = false, motorInitN = false, motorInitB = false;

    /**
     * pcb板的查询存储
     */
    public List<Byte> ioStatusDto02 = new ArrayList<>();
    public int southCount = 0, northernCount = 0;
    public int doorStatus = -1;
    /**
     * 抓取料袋的坐标
     */
    int[] pickLocation = new int[3];
    /**
     * 读取的IO数为 -- 控制在48个Io
     */
    private final String ioNum = "30";
    @Resource
    private BorunteCmdSendService borunteCmdSendService;
    @Resource
    private BorunteCmdHandleDaoImpl borunteCmdHandleDao;
    @Resource
    private BukePortCmdHandle bukePortCmdHandle;
    @Resource
    private MTPortCmdHandle mtPortCmdHandle;
    @Resource
    private IOCmdSendService ioCmdSendService;
    @Resource
    private IOCmdHandleDaoImpl ioCmdHandleDao;
    @Resource
    private CameraEastService cameraService;
    @Resource
    private CameraRepository cameraRepository;
    /**
     * 当前文件名称
     */
    private String currentFileName;
    /**
     * 激光测距仪
     */
    @Resource
    private MTRangingCmdSendService mtRangingRun;
    List<NewMemDevice.CheckInfo> checkInfoList = new ArrayList<>();
    @Resource
    MeichCameraCmdHandleDao meichCameraCmdHandleDao;
    @Resource
    public ThreadPoolTaskScheduler taskScheduler1;
    int reTryCount = 1;

    /**
     * 东区加药 - 上电初始化
     */
    public void init() {
        /*
            1. 初始化IO模块网口
            2. 初始化步科电机串口
            3. 初始化伯朗特机器人网口
            4. 初始化测距仪串口
         */
        ioCmdHandleDao.init("192.168.1.108:502");
        borunteCmdHandleDao.init("192.168.4.4:502");
        meichCameraCmdHandleDao.init();
        mtPortCmdHandle.addPort("1");

//        ioCmdHandleDao.init("127.0.0.1:502");
//        borunteCmdHandleDao.init("127.0.0.1:503");
//        meichCameraCmdHandleDao.init();
//        mtPortCmdHandle.addPort("1");

        DeviceAction byDeviceId = deviceService.findByDeviceId(1);
        northernCount = byDeviceId.getC1();
        southCount = byDeviceId.getC2();

        checkInfoList.add(new NewMemDevice.CheckInfo("机器人连接检测", 0));
        checkInfoList.add(new NewMemDevice.CheckInfo("3D相机连接检测", 0));
        checkInfoList.add(new NewMemDevice.CheckInfo("远程IO连接检测", 0));
        checkLink();
        newNemDevice.setCheckInfos(checkInfoList);
        newNemDevice.setSouthCount(southCount + "");
        newNemDevice.setNorthernCount(northernCount + "");
        newNemDevice.setStatusDoor(doorStatus == 1 ? "1" : "0");
        newNemDevice.setRobotInit(robotInit ? "1" : "0");
        newNemDevice.setStatusStart(isStart ? "1" : "0");
        newNemDevice.setStatusRunning(isRunning ? "1" : "0");

//        taskScheduler1.execute(new ProcessTask(-1, 200) {
//            @Override
//            protected void doTask() {
//                newNemDevice.setSouthCount(southCount + "");
//                newNemDevice.setNorthernCount(northernCount + "");
//                newNemDevice.setStatusDoor(doorStatus == 1 ? "1" : "0");
//                newNemDevice.setRobotInit(robotInit ? "1" : "0");
//                newNemDevice.setStatusStart(isStart ? "1" : "0");
//                newNemDevice.setStatusRunning(isRunning ? "1" : "0");
//            }
//        });

        /*
            1. 开启读取IO模块信号
         */
        ioCmdSendService.readAllCoil02(ioNum, bytes -> {
            ioStatusDto02 = bytes;
            log.info("IO信号为:" + bytes);
            StringBuilder ioSensor = new StringBuilder("0000000000000000");
            if (bytes.get(8) == 0) {
                log.info("料盒-北料斗报警");
                ioSensor.replace(15, 16, "1");
                eastDosingProcess(8);
            } else if (bytes.get(14) == 0) {
                log.info("料盒-南料斗报警");
                ioSensor.replace(14, 15, "1");
                eastDosingProcess(14);
            }
            List<Byte> listSensor = new ArrayList<>();
            listSensor.add(bytes.get(8));
            listSensor.add(bytes.get(14));
            int i = Integer.parseInt(ioSensor.toString(), 2);
            eastDataResp.setRobotDataThree(HexUtil.toHex(i));
            newNemDevice.setListSensor(listSensor);
            newNemDevice.setStatusRunning(isRunning ? "1" : "0");
            if (bytes.get(18) == 1) {
                if (doorStatus != 1) {
                    openDoor();
                }
            } else {
                if (doorStatus == 1) {
                    closeDoor();
                }
            }
            newNemDevice.setStatusDoor(doorStatus == 1 ? "1" : "0");
            newNemDevice.setRobotInit(robotInit ? "1" : "0");
            newNemDevice.setStatusStart(isStart ? "1" : "0");
            /*
                判断是否是在调试状态 如果在就退出流程
            */
            if (!isTest && isStart) {
                if (bytes.get(8) == 0) {
                    log.info("料盒-北料斗报警");
                    eastDosingProcess(8);
                } else if (bytes.get(14) == 0) {
                    log.info("料盒-南料斗报警");
                    eastDosingProcess(14);
                }
            }
        });
        /*
            1. 写入空压机开启
            2. 写入压板上合
            3. 写入夹爪闭合
         */
        ioCmdSendService.ioWriteCoil05(0, true, s -> log.info("机器人-空压机开启完成"));
        ioCmdSendService.ioWriteCoil05(3, false, s -> log.info("机器人-压板上合完成"));
        DelayUtil.delayedExecution(200, () -> ioCmdSendService.ioWriteCoil05(1, false, s -> log.info("机器人-夹爪闭合完成")));
        /*
            判断电机是否进行初始化，如果没有，则电机进行初始化
         */
        motorInitB = true;
        motorInitN = true;
    }

    public void checkLink() {
        borunteCmdSendService.borunteReadLocation(new CommonCallback<int[]>() {
            @Override
            public void statusCallback(int[] ints) {
                checkInfoList.get(0).setStatus(1);
            }
        });
        ioCmdSendService.readAllCoil01("1", new CommonCallback<List<Byte>>() {
            @Override
            public void statusCallback(List<Byte> bytes) {
                checkInfoList.get(2).setStatus(1);
            }
        });
        meichCameraCmdHandleDao.startCamera(new CommonCallback<String>() {
            @Override
            public void statusCallback(String s) {
                checkInfoList.get(1).setStatus(1);
            }
        }, 1);

    }

    public void testRun(int type) {
        isRunning = true;
        DelayUtil.delayedExecution(10000, new Runnable() {
            @Override
            public void run() {
                if (type == 0) {
                    southCount++;
                } else {
                    northernCount++;
                }
                isRunning = false;
            }
        });
    }

    public void openDoor() {
        if (isRunning) {
            for (NewMemDevice.CheckInfo checkInfo : checkInfoList) {
                checkInfo.setStatus(0);
            }
            // 非正常关闭情况下 需要重新初始化 检测
            robotInit = false;
            borunteCmdSendService.stopProcess();
        }
        isStart = false;
        doorStatus = 1;
    }

    public void closeDoor() {
        if (robotInit) {
            reStart();
            isStart = true;
            log.info("IO门关闭后的操作");
        } else {
            checkLink();
        }
        doorStatus = -1;
    }


    public void initRobot() {
        if (!robotInit) {
            processRobot = 2;
            nextIntRobot();
        }
    }

    /**
     * 东区加药 - 流程开始入口
     */
    public void eastDosingProcess(int id) {
        log.info("==========================当前不为测试模式，自动流程开始了==========================");
        /* 判断初始化是否完成 */
        if (!motorInitB || !motorInitN || !robotInit) {
            log.info("当前初始化状态，motor N&B+" + motorInitN + motorInitB + ",robot+" + robotInit);
            return;
        }

        /* 判断当前是否有流程正在进行 */
        if (isRunning) {
            return;
        }
        isRunning = true;
        /* 保存报警id */
        warnId = id;

        log.info("当前料盒id为:" + id + ",即将判断是否有料--------------------->");
        /* West-Data-添加当前缺料位置 */
        if (id == 8) {
            eastDataResp.setRobotDataTwo("0001");
        } else {
            eastDataResp.setRobotDataTwo("0002");
        }
        processRobot = 0;
        nextIntRobot();
    }

    public NewMemDevice newNemDevice = new NewMemDevice();

    public static int orationType = -1;


    public void reStart() {
        processRobot = -1;
        processMotor = -1;
        isRunning = false;
        warnId = -1;
        eastDataResp.setRobotDataTwo("0");
    }

    public static EastDataResp eastDataResp = new EastDataResp();

    /**
     * 東區加藥-伯朗特機器人流程
     */
    public void nextIntRobot() {
        /* West-Data-添加实时数据 */
        if (processRobot == 0) {
            eastDataResp.setRobotDataOne("0000");
            log.info("待机");
        } else if (processRobot == 4) {
            eastDataResp.setRobotDataOne("0001");
            log.info("正上方");
        } else if (processRobot == 6) {
            eastDataResp.setRobotDataOne("0002");
            log.info("抓料");
        } else if (processRobot == 7) {
            eastDataResp.setRobotDataOne("0003");
            log.info("放料");
        } else if (processRobot == 9) {
            eastDataResp.setRobotDataOne("0004");
            log.info("扔袋");
        }
        /* 机器人进度 */
        processRobot++;
        /* 判断机器人当前进度 */
        switch (processRobot) {
            /* case 1,2 为相机拍照流程 */
            case 1 -> {
                /* 控制相机拍照 */
                meichCameraCmdHandleDao.startCamera(s -> {
                    s = AsciiAfterService.hexToAscii(s);
                    s = s.trim();
                    log.info("相机识别结果" + s);
                    String[] split = s.split(",");
                    if (!split[0].equals("1")) {
                        log.info("相机识别结果" + s + ">>>>split[0]" + split[0]);
                        if (split[0].equals("4")) {
                            log.info("无料...");
                        } else if (split[0].equals("5")) {
                            reTryCount++;
                            if (reTryCount >= 5) {
                                log.info("重试四次后仍然无结果..需要人工介入!!!!!!!");
                                return;
                            }
                            log.info("无结果...需要重试" + reTryCount);
                            processRobot = 0;
                            nextIntRobot();
                        }
                        return;
                    }
                    reTryCount = 1;
                    for (int i = 0; i < 3; i++) {
                        pickLocation[i] = (int) (Float.parseFloat(split[i + 2]) * 1000);
                        if (i == 2) {
                            pickLocation[i] += 320 * 1000;
                        }
                    }
                    BorunteCmdHandleDaoImpl.UVW[2] = (int) (Float.parseFloat(split[7]) * 1000) + 90000;
                    nextIntRobot();
                }, 1);
                /* 遍历文件夹是否为最新 */
            }
            case 2 -> {
                /*处理照片结果*/

                /*处理完成之后，将照片剪切过去*/


                /* 拿到结果之后，袋数才会+1 */

                processRobot = 3;
                nextIntRobot();
            }
            /* case 3 为机器人初始化流程 */
            /* 机器人初始化 - 读取机器人世界坐标,设置原点Z轴高度 */
            case 3 -> {
                /*
                    1. 设置机器人原点Z轴高度
                    2. 写入M12回原点信号
                    3. 读取M22回原点完成信号
                 */
                borunteCmdSendService.borunteSetOriginZ(s -> {
                    borunteCmdSendService.borunteWriteStatusM(162, true);
                    log.info("机器人-机械臂 初始化 M12");
                    borunteCmdSendService.borunteReadStatusM(170, 3000, s1 -> {
                        borunteCmdSendService.borunteWriteStatusM(170, false);
                        robotInit = true;
                        newNemDevice.setRobotInit("1");
                        log.info("机器人-机械臂 初始化完成 M22");
                    });
                });

            }
            /* case 4 机器人流程正式开始 */
            /* 1. 机器人-机械臂 移动到料袋准备抓料 */
            case 4 -> {
                /*
                    1.1 写入抓料点位置 , 写入抓料点正上方位置
                    1.2 通知信号 - M10 - 开始抓料
                    1.3 读取信号 - M121 - 等待通知夹料气缸夹紧
                 */
                borunteCmdSendService.borunteSetPickLocation(pickLocation);
                log.info("设置机器人Z轴坐标高度完成");
                DelayUtil.delayedExecution(500, () -> {
                    ioCmdSendService.ioWriteCoil05(1, true, s14 -> log.info("机械爪打开等待抓料"));
                    DelayUtil.delayedExecution(1000, () -> {
                        borunteCmdSendService.borunteWriteStatusM(160, true);
                        log.info("机器人-机械臂 写入M10 开始抓料信号");
                        /* 伯朗特机器人-读取M15信号，到达Z轴高度 */
                        nextIntRobot();
                    });
                });
            }
            case 5 -> {
                // 0. 读取信号 M121 -- 等待通知夹料气缸夹紧
                borunteCmdSendService.borunteReadStatusM(201, 3000, s1 -> {
                    borunteCmdSendService.borunteWriteStatusM(201, false);
                    nextIntRobot();
                    log.info("机器人-机械臂 读取M121 读取气缸夹紧");
                });
            }
            /* 2. 机器人-机械臂 等待信号抓具夹紧 */
            case 6 -> {
                /*
                    2.1 IO控制 - 控制气缸夹紧
                    2.2 回应信号 - M111 - 写入气缸夹紧完成
                    2.3 读取信号 - M20 - 等待抓料完成信号
                 */
                ioCmdSendService.ioWriteCoil05(1, false, s -> {
                    log.info("IO控制-机器人气缸夹紧完成");
                    borunteCmdSendService.borunteWriteStatusM(193, true);
                    log.info("机器人-机械臂 写入M111信号 气缸夹紧完成");
                });

                /*
                    1.4 电机流程开始 - 打开料斗盖子准备好
                    1.5 打开机械爪准备抓料
                 */
//                processMotor = 0;
//                if (warnId == 8) {
//                    nextIntMotorB();
//                }
//                if (warnId == 14) {
//                    nextIntMotorN();
//                }

                borunteCmdSendService.borunteReadStatusM(168, 3000, s -> {
                    borunteCmdSendService.borunteWriteStatusM(168, false);
                    nextIntRobot();
//                    processRestart();
                    log.info("机器人-机械臂 读取M20 读取抓料完成信号");
                });
            }
            /* 3. 机器人-机械臂 等待抓料完成-通知机器人去放料 */
            case 7 -> {
                /*
                    3.1 通知信号 - M11 - 通知放料开始
                    3.2 读取信号 - M124 - 读取抖料信号
                 */
                if (warnId == 8) {
                    borunteCmdSendService.borunteWriteStatusM(161, true);
                    log.info("机器人-机械臂 写入M11信号 通知放料开始");
                }
                if (warnId == 14) {
                    borunteCmdSendService.borunteWriteStatusM(163, true);
                    log.info("机器人-机械臂 写入M13信号 通知放料开始");
                }

                borunteCmdSendService.borunteReadStatusM(204, 3000, s -> {
                    borunteCmdSendService.borunteWriteStatusM(204, false);
                    nextIntRobot();
                    log.info("机器人-机械臂 读取M124 读取通知抖料信号");
                });
            }
            /* 4. 机器人-机械臂 等待抖料信号-抓具抖动 */
            case 8 -> {
                /*
                    4.1 IO控制 - 控制夹具往返抖动4次
                    4.2 回应信号 - M114 - 抖料完成
                    4.3 读取信号 - M21 - 放料完成信号
                 */
                ioCmdSendService.ioWriteCoil05(5, true, s -> ioCmdSendService.ioWriteCoil05(5, false, s1 -> ioCmdSendService.ioWriteCoil05(5, true, s11 -> ioCmdSendService.ioWriteCoil05(5, false, s1112 -> ioCmdSendService.ioWriteCoil05(5, true, s111 -> ioCmdSendService.ioWriteCoil05(5, false, s1111 -> ioCmdSendService.ioWriteCoil05(5, true, s2 -> ioCmdSendService.ioWriteCoil05(5, false, s21 -> {
                    ioCmdSendService.ioWriteCoil05(5, true, s33 -> {
                        ioCmdSendService.ioWriteCoil05(5, false, s34 -> ioCmdSendService.ioWriteCoil05(5, true, s341 -> ioCmdSendService.ioWriteCoil05(5, false, s34112 -> ioCmdSendService.ioWriteCoil05(5, true, s3411 -> ioCmdSendService.ioWriteCoil05(5, false, s34111 -> ioCmdSendService.ioWriteCoil05(5, true, s3411112 -> ioCmdSendService.ioWriteCoil05(5, false, s341111 -> {
                            borunteCmdSendService.borunteWriteStatusM(196, true);
                            log.info("机器人-机械臂 写入M114信号 回应抖料完成");
                            borunteCmdSendService.borunteReadStatusM(169, 3000, s3 -> {
                                ioCmdSendService.ioWriteCoil05(169, false, new CommonCallback<String>() {
                                    @Override
                                    public void statusCallback(String s4) {
                                        nextIntRobot();
                                        log.info("机器人-机械臂 读取M21 读取放料完成信号");
                                    }
                                });
                            });
                        },600),600),600),600),600),600),600);
                    },600);
                }, 600), 600), 600), 600), 600), 600), 600), 600);

            }
            /* case 8 电机流程正式开始 */
            /* 5. 机器人-机械臂 等待夹具松开信号 */
            case 9 -> {
                /*
                    电机流程开始-盖子合上
                 */
//                processMotor = 2;
//
//                if (warnId == 8) {
//                    nextIntMotorB();
//                }
//
//                if (warnId == 14) {
//                    nextIntMotorN();
//                }
                /*
                    5.1 读取信号 - M120 - 通知夹具松开
                 */
                borunteCmdSendService.borunteReadStatusM(200, 3000, s -> {
                    borunteCmdSendService.borunteWriteStatusM(200, false);
                    nextIntRobot();
                    log.info("机器人-机械臂 读取M120 读取通知夹具松开");
                });
            }
            /* 6. 机器人-机械臂 控制夹具松开 */
            case 10 -> {
                /*
                    6.1 IO控制 - 控制夹具松开
                    6.2 回应信号 - M110 - 夹具松开完成
                    6.3 等待信号 - M122 - 气缸下推信号
                 */
                ioCmdSendService.ioWriteCoil05(1, true, s -> {
                    borunteCmdSendService.borunteWriteStatusM(192, true);
                    log.info("机器人-机械臂 写入M110信号 回应夹具松开完成");
                    borunteCmdSendService.borunteReadStatusM(202, 3000, s12 -> {
                        borunteCmdSendService.borunteWriteStatusM(202, false);
                        nextIntRobot();
                        log.info("机器人-机械臂 读取M122 读取通知气缸下推动作");
                    });
                });
            }
            /* 7. 机器人-机械臂 控制气缸下推 */
            case 11 -> {
                /*
                    7.1 IO控制 - 控制气缸下推
                    7.2 回应信号 - M112 - 气缸下推完成
                    7.3 等待信号 - M123 - 等待气缸上回动作
                 */
                ioCmdSendService.ioWriteCoil05(3, true, s -> {
                    borunteCmdSendService.borunteWriteStatusM(194, true);
                    log.info("机器人-机械臂 写入M112 回应气缸下推动作完成");
                    borunteCmdSendService.borunteReadStatusM(203, 3000, s13 -> {
                        borunteCmdSendService.borunteWriteStatusM(203, false);
                        nextIntRobot();
                        log.info("机器人-机械臂 读取M123 读取通知气缸下推动作");
                    });
                });
            }
            /* 8. 机器人-机械臂 等待IO压板到位 */
            case 12 -> {
                ioCmdSendService.ioReadCoil02(ioNum, 1, 0, 3000, s -> {
                    nextIntRobot();
                    log.info("IO控制-压板到位");
                });
            }
            /* 9. 机器人-机械臂 控制气缸上回 */
            case 13 -> {
                /*
                    9.1 IO控制 - 控制气缸上回
                    9.2 回应信号 - M113 - 气缸上回完成
                 */
                /* 9.3 IO控制 - 流程结束后，默认关闭机械爪 */

                ioCmdSendService.ioWriteCoil05(3, false, s -> ioCmdSendService.ioWriteCoil05(1, false, s1 -> ioCmdSendService.ioWriteCoil05(1, true, s11 -> ioCmdSendService.ioWriteCoil05(3, true, s1112 -> ioCmdSendService.ioWriteCoil05(3, false, s111 -> ioCmdSendService.ioWriteCoil05(1, false, s1111 -> ioCmdSendService.ioWriteCoil05(1, true, s2 -> ioCmdSendService.ioWriteCoil05(3, true, s21 -> {
                    ioCmdSendService.ioWriteCoil05(3, false, s32 -> ioCmdSendService.ioWriteCoil05(1, false, s321 -> {
                        borunteCmdSendService.borunteWriteStatusM(195, true);
                        log.info("机器人-机械臂 写入M113信号 回应气缸上回到位");
                        nextIntRobot();
                    }), 1000);
                }, 1000), 1000), 1000), 1000), 1000), 1000), 1000), 1000);
            }
            case 14 -> {
                DelayUtil.delayedExecution(30000, () -> {
                    if (warnId == 8) {
                        southCount++;
                        newNemDevice.setSouthCount(southCount + "");
                    } else {
                        newNemDevice.setSouthCount(northernCount + "");
                    }
                    deviceService.updateByDeviceId(southCount,northernCount,0,0);
                    processFinish();
                    log.info("机器人-流程结束");
                });
            }
            default -> log.info("机器人-流程出现异常");
        }
    }

    public void cassetteCountZero(){
        northernCount = 0;
        southCount = 0;
        deviceService.updateByDeviceId(southCount,northernCount,0,0);
    }

    @Resource
    private IOCmdSendService deviceService;


    /**
     * 判断流程结束信号
     */
    public static void processFinish() {
        if (processRobot >= 14) {
//        if (processRobot >= 6) {
            processRobot = -1;
            processMotor = -1;
            isRunning = false;
            warnId = -1;
            eastDataResp.setRobotDataTwo("0");
        }
    }

    public static void processRestart() {
        processRobot = -1;
        processMotor = -1;
        isRunning = false;
        warnId = -1;
        eastDataResp.setRobotDataTwo("0");
    }

    public void startCamera() {
        meichCameraCmdHandleDao.startCamera(s -> {
            s = AsciiAfterService.hexToAscii(s);
            s = s.trim();
            log.info("相机识别结果" + s);
            String[] split = s.split(",");
            if (!split[0].equals("1")) {
                log.info("相机识别结果" + s + ">>>>split[0]" + split[0]);
                if (split[0].equals("4")) {
                    log.info("无料...");
                } else if (split[0].equals("5")) {
                    reTryCount++;
                    if (reTryCount >= 5) {
                        log.info("重试四次后仍然无结果..需要人工介入!!!!!!!");
                    }
                    log.info("无结果...需要重试" + reTryCount);
                    startCamera();
                }
                return;
            }
            reTryCount = 1;
            for (int i = 0; i < 3; i++) {
                pickLocation[i] = (int) (Float.parseFloat(split[i + 2]) * 1000);
                if (i == 2) {
                    pickLocation[i] += 400 * 1000;
                }
            }
            BorunteCmdHandleDaoImpl.UVW[2] = (int) (Float.parseFloat(split[7]) * 1000) + 90000;
            borunteCmdHandleDao.borunteSetPickPoint(pickLocation);
            borunteCmdHandleDao.borunteSetPickZ(pickLocation);
            borunteCmdHandleDao.borunteWriteSwitch(160, true);
        }, reTryCount);
    }
}
